﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;

namespace Project5188.BUS.Operations
{
    public class PersonnelActions : DetailView<personnelaction>
    {
        private project5188Entities _context;
        // ReSharper disable InconsistentNaming
        private new List<personnelaction> EntityList;
        private new personnelaction Entity;
        // ReSharper restore InconsistentNaming

        public long TransactionId { get; private set; }

        #region Overrides of DetailView<personnelaction>

        public override List<personnelaction> GetEntityList()
        {
            _context = new project5188Entities();
            EntityList = _context.personnelactions.Where(a => !a.IsDeleted).OrderBy(a => a.a_ID).ToList();

            return EntityList;
        }

        public override List<personnelaction> GetEntityById(long id)
        {
            _context = new project5188Entities();
            EntityList = _context.personnelactions.Where(a => a.a_ID == id && !a.IsDeleted).OrderBy(a => a.a_ID).ToList();

            return EntityList;
        }

        public override personnelaction GetEntity()
        {
            _context = new project5188Entities();
            Entity = new personnelaction();
            return Entity;
        }

        public override void Update()
        {
            _context.SaveChanges();
        }

        public override void Insert()
        {
            _context.AddTopersonnelactions(Entity);
            _context.SaveChanges();
            TransactionId = Entity.a_ID;
        }

        #endregion

        public List<personnelaction> GetEntityListByParameters(string registryNoText, string nameSurname, string tcNoText, DateTime? holidaysStartDate, DateTime? holidaysEndDate, string usedHolidaysText, string remainingHolidayText)
        {
            _context = new project5188Entities();
            int tcNo = 0, registryNo = 0;
            int usedHolidays = 0, remainingHoliday = 0;

            if (!String.IsNullOrEmpty(tcNoText) && !String.IsNullOrWhiteSpace(tcNoText))
            {
                Int32.TryParse(tcNoText, out tcNo);
            }

            if (!String.IsNullOrEmpty(registryNoText) && !String.IsNullOrWhiteSpace(registryNoText))
            {
                Int32.TryParse(registryNoText, out registryNo);
            }

            if (!String.IsNullOrEmpty(usedHolidaysText) && !String.IsNullOrWhiteSpace(usedHolidaysText))
            {
                Int32.TryParse(usedHolidaysText, out usedHolidays);
            }

            if (!String.IsNullOrEmpty(remainingHolidayText) && !String.IsNullOrWhiteSpace(remainingHolidayText))
            {
                Int32.TryParse(remainingHolidayText, out remainingHoliday);
            }

            var objectParameters = new ObjectParameter[7];

            var registryParameter = new ObjectParameter("RegistryNo", typeof(int)) { Value = registryNo };
            objectParameters[0] = registryParameter;

            var nameSurnameParameter = new ObjectParameter("NameSurname", typeof(string)) { Value = nameSurname };
            objectParameters[1] = nameSurnameParameter;

            var tcNoParameter = new ObjectParameter("TCNo", typeof(int)) { Value = tcNo };
            objectParameters[2] = tcNoParameter;

            var holidayStartDateParameter = new ObjectParameter("HolidaysStartDate", typeof(DateTime?)) { Value = holidaysStartDate };
            objectParameters[3] = holidayStartDateParameter;

            var holidayEndDateParameter = new ObjectParameter("HolidaysEndDate", typeof(DateTime?)) { Value = holidaysStartDate };
            objectParameters[4] = holidayEndDateParameter;

            var usedHolidaysParameter = new ObjectParameter("UsedHolidays", typeof(int)) { Value = usedHolidays };
            objectParameters[5] = usedHolidaysParameter;

            var remainingHolidayParameter = new ObjectParameter("RemainingHoliday", typeof(int)) { Value = remainingHoliday };
            objectParameters[6] = remainingHolidayParameter;

            var list = _context.ExecuteFunction<personnelaction>("GetPersonnelActionListByParameters", objectParameters).ToList();

            return list;
        }
    }
}
